package com.qf.wangqing.sportsnews.fragment.headline;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.qf.wangqing.sportsnews.MyApplication;
import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.adapter.headline.AdapterDanymicRec;
import com.qf.wangqing.sportsnews.bean.HeadEntity.DanymicEntityTop;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class DanymicFragment extends Fragment implements Callback<DanymicEntityTop> {

    AdapterDanymicRec adapter;
    private RecyclerView recycler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_danymic, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = (RecyclerView) view.findViewById(R.id.danymic_recycler);

        MyApplication application = (MyApplication) getActivity().getApplication();
        application.getHeadNetService().getDanymic().enqueue(this);
    }

    @Override
    public void onResponse(Call<DanymicEntityTop> call, Response<DanymicEntityTop> response) {
        if (response.body().getData() != null) {
            adapter=new AdapterDanymicRec(getContext());
            recycler.setAdapter(adapter);
            adapter.addAll(response.body().getData().getSnapshots());
        }
    }
    @Override
    public void onFailure(Call<DanymicEntityTop> call, Throwable t) {
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
